var own = {
    /**
     * 配置文件路径
     */
    configPath: '/js/own/own.config.js',
    /**
     * 初始化加载
     */
    init: function () {
        /**
         * 当前own.js所在文件夹
         */
        var nowPath = own._getRootPath();
        var configPath = nowPath + "own.config.js";
        /**
         * 当前项目跟路径，两种情况，存在项目名与不存在项目名
         * @type {string}
         */
        var localhostPath = "";
        //判断浏览器版本
        own._load(configPath, function () {
            //获取第一条配置信息，根据第一条配置信息截获出项目名对应信息

            var utilPath = own.config.modules.util.substring(1);
            var root = utilPath.substring(0, utilPath.indexOf('/'));
            localhostPath = nowPath.substring(0, nowPath.indexOf('/' + root + '/'));

            own.config.localhostPath = localhostPath;

            own._load(localhostPath + own.config.modules.owncss);
            own._load(localhostPath + own.config.modules.velocity);
            own._load(localhostPath + own.config.modules.event);
            own._load(own.config.modules.util, function () {
                own._load(own.config.modules.request, function () {
                    document.onload = document.onreadystatechange = function () {
                        if (document.readyState == "complete") {
                            var objs = own._getScriptObjct();
                            //如果全局只引入一个js,就拦截直接获取
                            var src_ = objs.src;
                            var srccnt = src_.lastIndexOf('?');
                            var moduleName = '';
                            if (srccnt != -1) {
                                moduleName = src_.substr(srccnt + 1);
                            }
                            $$.request("browser", function () {
                                var version = own.browser.getVersion();
                                if (version == own.browser.version.IE8 || version == own.browser.version.IE9) {
                                    $$.request("html5,respond");
                                }
                                if (srccnt == -1) {
                                    eval(objs.innerText);
                                }
                            });
                            if (srccnt != -1 && $$.util.trim(moduleName) != '') {
                                $$.request(moduleName,objs.innerText);
                            }
                        }
                    }
                }, own.config.localhostPath);
            }, own.config.localhostPath);
        });
    },
    /**
     * 获取当前own.js运行目录对应的文件夹
     * @returns {*}
     * @private
     */
    _getRootPath: function () {
        var src = null;
        try {
            array[1];
        } catch (e) {
            var stack = e.stack || e.sourceURL || e.stacktrace || '';
            var rgx = /(?:http|https|file):\/\/.*?\/.+?\.js/;
            src = (rgx.exec(stack) || [])[0] || '';
            if (src != null) {
                src = src.substring(0, src.lastIndexOf('/') + 1);
            }
        }
        if (src == null) {
            var doc = document;
            var jsPath = doc.currentScript ? doc.currentScript.src : function () {
                var js = doc.scripts
                    , last = js.length - 1
                    , src;
                for (var i = last; i > 0; i--) {
                    if (js[i].readyState === 'interactive') {
                        src = js[i].src;
                        break;
                    }
                }
                return src || js[last].src;
            }();
            src = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
        }
        return src;
    },
    /**
     * 加载js
     * @param url
     * @param fun
     */
    _load: function (url, fun, path) {
        if (url.lastIndexOf('.js') != -1) {
            var obj = document.createElement('script');
            obj.type = 'text/javascript';
            if (path) {
                url = path + url;
            }
            obj.id = url;
            obj.charset = 'UTF-8';
            obj.src = url;
            obj.onload = obj.onreadystatechange = function () {
                if (!obj.readyState || obj.readyState === "loaded" || obj.readyState === "complete") {
                    if(typeof(fun) == 'function'){
                        fun();
                    }
                }
            }
        } else {
            var obj = document.createElement('link');
            obj.rel = 'stylesheet';
            if (typeof(own.util) == 'object') {
                obj.id = own.util.hashCode(url);
            } else {
                obj.id = url;
            }
            obj.charset = 'UTF-8';
            obj.href = url;
        }
        document.head.appendChild(obj);
    },
    _getScriptObjct: function () {
        var scriptSrc = document.getElementsByTagName('script');
        for (var i = 0, cnt = scriptSrc.length; i < cnt; i++) {
            if (scriptSrc[i].src.indexOf('own.js') >= 0) {
                return scriptSrc[i];
            }
        }
    }
};

window.$own = $$ = own;
//执行初始化操作
own.init();